using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Blowfish_NET;

using Microsoft.Win32;

namespace GSync
{
    class Credential
    {
        private String userName;
        static String sKey = "fabio123";
        static Blowfish mBlowfish = null;
        static System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

        public String UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private String password;

        public String Password
        {
            get { return password; }
            set { password = value; Setted = true; }
        }
        private Boolean setted=false;

        public Boolean Setted
        {
            get { return setted; }
            set { setted = value; }
        }

        public Credential()
        {
            this.userName = "@gmail.com";
            this.password = "";
            Setted = false;
            
            byte[] key = encoding.GetBytes(sKey);
            mBlowfish = new Blowfish(key);
        }

        public static Credential load(String defUser)
        {
            if (mBlowfish == null)
            {
                byte[] key = encoding.GetBytes(sKey);
                mBlowfish = new Blowfish(key);
            }
            Credential cr = new Credential();
            RegistryKey reg = Registry.CurrentUser.OpenSubKey(Config.CONFIG_PATH);
            reg = reg.OpenSubKey(defUser);
            if (reg == null)
            {
                return cr;
            }
            byte[] input=null;
            String username=null;
            try
            {
                input = (byte[])reg.GetValue(Config.ACL);
                username = (String)reg.GetValue(Config.USER_NAME);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return cr;
            }
            if (username != null && input != null)
            {
                int mCount = input.Length;
                int bound = ((mCount % 8) == 0) ? mCount : (((mCount / 8) + 1) * 8);
                mBlowfish.Decrypt(input, input, 0, 0, bound);
                String unCryptPass = encoding.GetString(input, 0, input.Length);
                
                String[] dividePass=unCryptPass.Split('Z');

                int size = Convert.ToInt16(dividePass[0]);
                String pass = unCryptPass.Substring(dividePass[0].Length+1, size);
                
                
                cr.UserName = username;
                cr.password = pass;
                cr.setted = true;
            }
            return cr;
        }

        private String normalizePass(String passwordString)
        {
            int extrC = 8-(passwordString.Length % 8);
            String result = passwordString;
            String filler = new String('0',10);
            if (extrC > 0)
            {
                result += filler.Substring(0, extrC);
            }
            return result;
        }

        public void save(String defUser)
        {
            String passwordString = this.password.Length +"Z"+ this.password;
            byte[] input = encoding.GetBytes(normalizePass(passwordString));
            int mCount = input.Length;
            int bound = ((mCount % 8) == 0) ? mCount : (((mCount / 8) + 1) * 8);
            try
            {
                mBlowfish.Encrypt(input, input, 0, 0, bound);
                //mBlowfish.Encrypt(input, output, 0, 0, input.Length);
                //mBlowfish.Decrypt(input, input, 0, 0, bound);
                //Debug.WriteLine(encoding.GetString(input, 0, input.Length));
                RegistryKey root = Registry.CurrentUser.CreateSubKey(Config.CONFIG_PATH);
                RegistryKey reg = root.CreateSubKey(defUser);
                reg.SetValue(Config.ACL, input);
                reg.SetValue(Config.USER_NAME, this.userName); 
                //PocketRegistry.CERegister.RegSetStringValue(PocketRegistry.RegRoot.HKEY_CURRENT_USER, "pocketpc\\GCalSynch", "acl", encoding.GetString(input,0,input.Length));
                //PocketRegistry.CERegister.RegSetStringValue(PocketRegistry.RegRoot.HKEY_CURRENT_USER, "pocketpc\\GCalSynch", "user", this.userName);

            }
            catch (Exception e)
            {
                Debug.WriteLine(e.StackTrace);
                //throw e;
            }
        }

    }
}
